/*!
 * \file frhash2_del.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

typedef struct st_test {
    char *p_data;
    char *p_value;
} test_st;

static void __my_list_node_free(Frlist_node *node)
{
    frfree(((test_st *)node->data)->p_data);
    frfree(((test_st *)node->data)->p_value);
    frfree((test_st *)node->data);
}


static void __case1(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;

    i_ret = frhash2_del(p_hash_hdr, p_st_node, __my_list_node_free);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
}

static void __case2(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    p_hash_hdr->p_list_array = NULL;
    i_ret = frhash2_del(p_hash_hdr, p_st_node, __my_list_node_free);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    frfree(p_hash_hdr);
}


static void __case3(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;
    unsigned long ul_hash_size = 5;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;
    char *p_data_cmp =  NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_node = (test_st *)frmalloc(sizeof(test_st));

    //将"test"  "case"两个结构体存入hash
    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    ul_hash_key = frhash_key((unsigned char *)p_node->p_data,strlen(p_node->p_data));
    p_st_node = frlist_node_create((test_st *)p_node);
    frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node);

    p_node = (test_st *)frmalloc(sizeof(test_st));
    p_node->p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_node->p_data,strlen("case") + 1,"%s","case");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test3") + 1,"%s","test3");

    ul_hash_key = frhash_key((unsigned char *)p_node->p_data,strlen(p_node->p_data));
    p_st_node = frlist_node_create((test_st *)p_node);
    frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node);

    //得到"test"所在节点
    p_data_cmp = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data_cmp,strlen("test") + 1,"%s","test");
    ul_hash_key = frhash_key((unsigned char *)p_data_cmp,strlen(p_data_cmp));
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);

    //删除节点
    i_ret = frhash2_del(p_hash_hdr, p_st_node, __my_list_node_free);

    //查询"test"
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);
    CU_ASSERT_PTR_NULL(p_st_node);

    //查询"case"
    frfree(p_data_cmp);
    p_data_cmp = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data_cmp,strlen("case") + 1,"%s","case");
    ul_hash_key = frhash_key((unsigned char *)p_data_cmp,strlen(p_data_cmp));
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);
    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node->data)->p_value,"test3");

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frhash2_free(p_hash_hdr,__my_list_node_free);
    frfree(p_hash_hdr);
    frfree(p_data_cmp);
}

static void __case4(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;
    unsigned long ul_hash_size = 5;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;
    char *p_data_cmp =  NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_node = (test_st *)frmalloc(sizeof(test_st));

    //将"test"  "case"两个结构体存入hash
    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    ul_hash_key = frhash_key((unsigned char *)p_node->p_data,strlen(p_node->p_data));
    p_st_node = frlist_node_create((test_st *)p_node);
    frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node);

    p_node = (test_st *)frmalloc(sizeof(test_st));
    p_node->p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_node->p_data,strlen("case") + 1,"%s","case");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test3") + 1,"%s","test3");

    ul_hash_key = frhash_key((unsigned char *)p_node->p_data,strlen(p_node->p_data));
    p_st_node = frlist_node_create((test_st *)p_node);
    frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node);

    p_st_node = NULL;

    i_ret = frhash2_del(p_hash_hdr, p_st_node, __my_list_node_free);

    //查询"test"
    p_data_cmp = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data_cmp,strlen("test") + 1,"%s","test");
    ul_hash_key = frhash_key((unsigned char *)p_data_cmp,strlen(p_data_cmp));
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);
    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node->data)->p_value,"test1");

    frfree(p_data_cmp);

    //查询"case"
    p_data_cmp = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data_cmp,strlen("case") + 1,"%s","case");
    ul_hash_key = frhash_key((unsigned char *)p_data_cmp,strlen(p_data_cmp));
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);
    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node->data)->p_value,"test3");

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);

    frhash2_free(p_hash_hdr,__my_list_node_free);
    frfree(p_hash_hdr);
    frfree(p_data_cmp);
}

static void __case5(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    unsigned long ul_hash_size = 5;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;
    char *p_data_cmp =  NULL;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    p_node = (test_st *)frmalloc(sizeof(test_st));

    //将"test"  "case"两个结构体存入hash
    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    ul_hash_key = frhash_key((unsigned char *)p_node->p_data,strlen(p_node->p_data));
    p_st_node = frlist_node_create((test_st *)p_node);
    frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node);

    p_node = (test_st *)frmalloc(sizeof(test_st));
    p_node->p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_node->p_data,strlen("case") + 1,"%s","case");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test3") + 1,"%s","test3");

    ul_hash_key = frhash_key((unsigned char *)p_node->p_data,strlen(p_node->p_data));
    p_st_node = frlist_node_create((test_st *)p_node);
    frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node);

    p_data_cmp = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data_cmp,strlen("test") + 1,"%s","test");
    ul_hash_key = frhash_key((unsigned char *)p_data_cmp,strlen(p_data_cmp));
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);


    //查询"test"
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);


    frfree(p_data_cmp);

    //查询"case"
    p_data_cmp = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data_cmp,strlen("case") + 1,"%s","case");
    ul_hash_key = frhash_key((unsigned char *)p_data_cmp,strlen(p_data_cmp));
    p_st_node = frhash2_getLong(p_hash_hdr,ul_hash_key);
    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node->data)->p_value,"test3");

    frhash2_free(p_hash_hdr,__my_list_node_free);
    frfree(p_hash_hdr);
    frfree(p_data_cmp);
}

int add_suite_frhash2_del(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 frhash2_del()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}

